package com.seadragon.app.ksr.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.KsrConstants;

public class KsrInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
		Customer customer = (Customer)request.getSession().getAttribute(KsrConstants.SESSION_SCOPED_CUSTOMER);
		String uri = request.getRequestURI();
		if (uri.endsWith("signup.html")){
			if (customer!=null){//user logged in
				response.sendRedirect("/ksr/customer/profile.html");
				return false;			
			} else {
				return true;
			}
		} else if (uri.endsWith("login.html")){
			if (customer!=null){//user logged in
				response.sendRedirect("/ksr/customer/profile.html");
				return false;			
			} else {
				return true;
			}
		} else if (uri.endsWith("profile.html")){
			if (customer!=null){//user logged in
				return true;
			} else {
				response.sendRedirect("/ksr/login.html");
				return false;			
			}
		} 
		return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView mav) throws Exception {

    }
}
